/**
 * 检测元素所属类型
 * Object.prototype.toString.call(*)的可能结果如下所示：
 * @example null             [object Null]
 * @example undefined        [object Undefined]
 * @example Symbol());       [object Symbol]
 * @example true             [object Boolean]
 * @example ''               [object String]
 * @example 1                [object Number]
 * @example []               [object Array]
 * @example {}               [object Object]
 * @example new Date()       [object Date]
 * @example new Function()   [object Function]
 * @example new RegExp()     [object RegExp]
 * @example new Error()      [object Error]
 * @example document         [object HTMLDocument]
 * @example window           [object global] window 是全局对象 global 的引用
 * @param {*} ele 待检测的对象、字符串、数组等
 * @returns {String} 元素类型（string、number、boolean、symbol、undefined、null、function、date、array、object、regexp、error、htmldocument、global）
 */
export const typeOf = (ele) => {
  let endStr = Object.prototype.toString.call(ele);
  let type = endStr.split(" ")[1].slice(0, -1);
  return type;
};
/**
 * 把css的值（可能带单位，可能不带，例如em,rem,%等）转换下，如果已经带有单位，则返回原值，如果没有带单位，则加上rpx
 * @param  val {string,number}  数值
 * @param  unit {string} 单位
 * @returns
 */
export const toCssVal = (val = "", unit = "rpx") => {
  val = String(val);
  const regexp = /^[0-9.]+$/;
  const isOnlyNum = regexp.test(val);
  return isOnlyNum ? val + unit : val;
};
/**
 * 转换单位
 * @param type 转换类型，可选值：rpxToPx, pxToRpx
 */
export const convertUnit = (num, type = "rpxToPx") => {
  const baseWidth = 750; //基准宽度
  const { screenWidth } = wx.getSystemInfoSync();
  let ratio = 1;
  switch (type) {
    case "rpxToPx":
      ratio = screenWidth / baseWidth;
      break;
    case "pxToRpx":
      ratio = baseWidth / screenWidth;
      break;
    default:
      throw new Error("暂不支持此类型单位转换：" + type);
  }
  return num * ratio;
};
/**
 * 获取高亮词组
 * @param {string} str  要拆分的字符串
 * @param {string,array} key 关键词
 * @example 使用示例：
 * <block wx:for="{{help.getSplitWords(info.product_title, keyword)}}" wx:key="index">
 *     <text class="{{item.isKeyword ? 'light' : ''}}">{{item.value}}</text>
 * </block>
 */
export const getSplitWords = (str, key) => {
  str = str || "";
  key = key || "";
  var keys = typeof key === "string" ? [key] : key;
  var idx = -1,
    t = [],
    keyword = "";
  for (var i = 0; i < keys.length; i++) {
    keyword = keys[i];
    idx = str.indexOf(keyword);
    if (idx !== -1) {
      break;
    }
  }
  if (idx !== -1 && keyword !== "") {
    if (idx == 0) {
      t = getSplitWords(str.substring(keyword.length, str.length), keyword);
      t.unshift({ isKeyword: true, value: keyword });
      return t;
    } else if (idx > 0) {
      t = getSplitWords(str.substring(idx, str.length), keyword);
      t.unshift({ isKeyword: false, value: str.substring(0, idx) });
      return t;
    }
  }
  return [{ isKeyword: false, value: str }];
};
export const ObjectToString = (obj = {}, char = "&") => {
  let str = "";
  for (let key in obj) {
    str += `${char}${key}=${obj[key]}`;
  }
  return str.slice(1);
};
